﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Kaixin.Tools
{
    public class XMLParser
    {
        private XmlDocument doc = null;

        public XMLParser(string content)
        {
            doc = new XmlDocument();
            doc.LoadXml(content);
        }

        public XmlNodeList GetNodeList(string path)
        {
            string[] nodeNames = path.Split('/');
            return Parse(doc, nodeNames, 1);
        }

        public string GetNodeValue(string path)
        {
            try
            {
                return GetNodeList(path).Item(0).Value;
            }
            catch (Exception)
            {
                return "";
            }
        }


        private XmlNodeList Parse(XmlNode root, string[] nodeNames, int level)
        {
            XmlNodeList nodeList = root.ChildNodes;

		    if (level == nodeNames.Length)
			    return nodeList;

		    for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNode node = nodeList.Item(i);
			    if (node.Name.Equals(nodeNames[level])){
				    return Parse(node, nodeNames, ++level);
                }
            }		
		    return null;
        }
    }
}
